package com.ccb.sc.member.service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.ccb.sc.member.mapper.AppMemberPointsLogMapper;
import com.ccb.sc.member.vo.MemberPointsLogVo;
import com.ccb.sc.common.bind.page.PageParams;
import com.ccb.sc.common.bind.page.PageVo;
import com.ccb.sc.common.modules.member.entity.MemberPointsLog;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 会员积分记录 服务实现类
 * </p>
 *
 * @author ken
 * @since 2022-07-30
 */
@Service
public class AppMemberPointsLogService extends ServiceImpl<AppMemberPointsLogMapper, MemberPointsLog> {

    public PageVo<MemberPointsLogVo> searchByQueryParam(Long memberId, PageParams queryParam) {
        Page<MemberPointsLog> page = new Page<MemberPointsLog>(queryParam.getCurrent(), queryParam.getPageSize());
        Page<MemberPointsLog> pointsLogPage = this.page(page, Wrappers.<MemberPointsLog>lambdaQuery()
                .eq(MemberPointsLog::getMemberId, memberId)
                .orderByDesc(MemberPointsLog::getId));
        List<MemberPointsLog> records = pointsLogPage.getRecords();
        if (CollectionUtils.isEmpty(records)) {
            return PageVo.create(queryParam.getCurrent(), queryParam.getPageSize(), 0, Lists.newArrayList());
        }
        List<MemberPointsLogVo> list = records.stream().map(item -> {
            MemberPointsLogVo vo = new MemberPointsLogVo();
            vo.setLogName(item.getChangeType().getDesc());
            vo.setCreateTime(item.getCreateTime());
            vo.setAmount(item.getAmount().intValue());
            return vo;
        }).collect(Collectors.toList());

        return PageVo.create(queryParam.getCurrent(), queryParam.getPageSize(), page.getTotal(), list);
    }

}
